package com.zcsy.seller.web.store;
import java.sql.Time;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.manage.model.base.ShopBanner;
import com.zcsy.manage.model.base.ShopInfo;
import com.zcsy.manage.model.mall.GoodsInfo;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.base.IShopBannerService;
import com.zcsy.manage.service.base.IShopInfoService;
import com.zcsy.manage.service.mall.IGoodsInfoService;

/**
 * @Project: seller
 * @Title: PersonalInfoController
 * @Package com.zcsy.seller.web.storeInfo
 * @Description:个人信息控制层
 * @author fangli Fanglwj520@163.com
 * @date 2016年5月24日 上午9:45:49
 * @Copyright：2014 武汉恒数利通软件有限公司 All rights reserved.
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/storeInfo")
public class StoreInfoController {
    @Autowired
    private IShopInfoService shopInfoService;
    
    @Autowired
    private IShopBannerService shopBannerService;
    
    @Autowired
    private IGoodsInfoService goodsInfoService;
    
    private static final int SORT_DEFAULT = 0;
    
    /**对象类型为商品*/
	private static final int TYPE_OBJ_GOODS = 1;
	/**对象类型为网页*/
	private static final int TYPE_OBJ_WEB = 2;
	private static final int PAGE_SIZE =  5; //每页大小
	private static final int PAGE_NO   =  1;  //默认首页
    
    /**
     * @Description 根据ID查询出商铺信息
     * @param req
     * @return 
     * @author fangli
     * @date 2016年5月24日
     * @see
     */
    @RequestMapping(value = "/storeInfoList", method=RequestMethod.GET)
    public String shopInfoList(Model model, HttpServletRequest req){
        
           ShopInfo shopInfo = (ShopInfo)req.getSession().getAttribute(RequestConstants.SHOP_ID);
           ShopInfo storeInfo =  shopInfoService.selectByPrimaryKey(shopInfo.getId());
           model.addAttribute("storeInfo", storeInfo ); 

           return "store/storeinfo/storeInfoList";
    }
    
    /**
     * @Description 更新店铺信息<br>
     * @param descrip 描述说明
     * @param beginTime 开始时间
     * @param endTime 结束时间
     * @param phone 联系电话1
     * @param phone2 联系电话2
     * @param address 商铺地址
     * @param personalId 商铺信息主键id
     * @param request 用户请求
     * @param response 服务器响应
     * @author fangli 
     * @date 2016年5月24日
     * @see 
     */
    @RequestMapping(value = "UpdateStoreInfo", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson saveStoreInfo(String descrip,String beginTime,String endTime, String phone,String phone2,
    		String logo, String address, HttpServletRequest request,HttpServletResponse response) {
        
        RequestJson result = new RequestJson();
        
        ShopInfo shopInfo = (ShopInfo)request.getSession().getAttribute(RequestConstants.SHOP_ID);
        ShopInfo shop = shopInfoService.selectByPrimaryKey(shopInfo.getId());
        
        if(StringUtils.isNotEmpty(beginTime)) {
            Time dateOne = Time.valueOf(beginTime);
            shop.setBeginTime(dateOne);
        }
        if(StringUtils.isNotEmpty(endTime)) {
            Time dateTwo = Time.valueOf(endTime);
            shop.setEndTime(dateTwo);
        }
        
        try {
            shop.setDescrip(descrip);
            shop.setPhone(phone);
            shop.setPhone2(phone2);
            shop.setAddress(address);
            shop.setLogo(logo);
            shopInfoService.update(shop);
            request.getSession().setAttribute(RequestConstants.SHOP_ID, shop);
        }catch (Exception e) {
        	e.printStackTrace();
            result.setSuccess(false);
            result.setMsg("操作异常");
        }
        return result;
    } 
    
    /**
     * 
     * @Description 返回店铺装修列表页面<br>
     * 
     * @param pNo 当前页
     * @param title 标题
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年8月31日
     * @see
     */
    @RequestMapping(value = "/storeBannerList", method=RequestMethod.GET)
    public String storeBannerList(Model model,Integer pNo,String title, HttpServletRequest req){
    	if (pNo == null) {
    		pNo = PAGE_NO;
    	}
    	// 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
    	PageHelper.startPage(pNo, PAGE_SIZE);
    	
    	ShopBanner shopBanner = new ShopBanner();
    	if(StringUtils.isNotEmpty(title)){
    		shopBanner.setTitle(title);
		}

    	List<ShopBanner> shopBanners = shopBannerService.selectBannerByKeyword(shopBanner);
    	PageInfo<ShopBanner> info = new PageInfo<ShopBanner>(shopBanners);

    	model.addAttribute("shopBanners", info ); 
		model.addAttribute("currentPage", pNo);
    	model.addAttribute("title", title);
    	
    	return "store/storebanner/storeBannerList";
    }
    
    /**
     * 
     * @Description 返回店铺装修编辑页面<br>
     * 
     * @param id 主键id
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年8月31日
     * @see
     */
    @RequestMapping(value = "/storeBannerEdit", method=RequestMethod.GET)
    public String storeBannerEdit(Model model, String id,HttpServletRequest req){
    	try {
    			ShopBanner shopBanner = shopBannerService.selectByPrimaryKey(id);
    			if(shopBanner != null){
    				model.addAttribute("shopBanner", shopBanner);
    				model.addAttribute("objId", shopBanner.getObjId());
    			}
    		List<GoodsInfo> goodsInfos = goodsInfoService.getGoodsInfoByState();
    		model.addAttribute("goodsInfos", goodsInfos);

    	} catch (Exception e) {
    		e.printStackTrace();
    	}

    	return "store/storebanner/storeBannerEdit";
    }
    
    /**
     * 
     * @Description 返回店铺装修新增页面<br>
     * 
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年8月31日
     * @see
     */
    @RequestMapping(value = "/storeBannerAdd", method=RequestMethod.GET)
    public String storeBannerAdd(Model model, HttpServletRequest req) {
        ShopInfo shopInfo = (ShopInfo)req.getSession().getAttribute(RequestConstants.SHOP_ID);
        List<GoodsInfo> goodsInfos = goodsInfoService.getGoodsInfoByShopId(shopInfo.getId());
        model.addAttribute("goodsInfos", goodsInfos);
        
        return "store/storebanner/storeBannerAdd";
    }
    
    /**
     * 
     * @Description 保存或新增店铺装修记录<br>
     * 
     * @param title 标题
     * @param id 主键id
     * @param objType 对象类型：1为商品，2为特殊网页
     * @param objId 商品id
     * @param webUrl 网页地址
     * @param memo 备注
     * @param img 装修图片
     * @param req 用户请求
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年8月31日
     * @see
     */
    @RequestMapping(value="saveOrUpdateStoreBanner" , method = RequestMethod.POST)
    @ResponseBody
    public RequestJson saveOrUpdateStoreBanner(String title,String id,Integer objType,String objId,String webUrl,
    		String memo,String img,HttpServletRequest req){
    	RequestJson result = new RequestJson();
    	ShopBanner shopBanner = new ShopBanner();
    	shopBanner.setTitle(title);
    	shopBanner.setMemo(memo);
    	shopBanner.setObjType(objType);
    	shopBanner.setObjId(objId);
    	shopBanner.setImg(img);
    	shopBanner.setSort(SORT_DEFAULT);
    	ShopInfo shopInfo = (ShopInfo)req.getSession().getAttribute(RequestConstants.SHOP_ID);
    	shopBanner.setShopId(shopInfo.getId());
    	if(objType == TYPE_OBJ_GOODS){
    		GoodsInfo goodsInfo = goodsInfoService.getGoodsInfoByPrimaryKey(objId);
    		shopBanner.setGoodsLogo(goodsInfo.getLogo());
    		shopBanner.setGoodsName(goodsInfo.getName());
    		
    	} else if(objType == TYPE_OBJ_WEB){
    		shopBanner.setUrl(webUrl);
    		
    	}

    	User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
    	shopBanner.setUserAccount(user.getLoginname());

    	try {
    		if(id != ""&& !"0".equals(id)){
    			shopBanner.setId(id);
    			shopBanner.setUpdateTime(new Date());
    			shopBannerService.update(shopBanner);
    		}else{
    			shopBanner.setId(UniqueNumberUtils.get18UniqueNumber());
    			shopBanner.setCreateTime(new Date());
    			shopBannerService.insert(shopBanner);
    		}

    		result.setSuccess(true);
    	} catch (Exception e) {
    		e.printStackTrace();
    		result.setMsg("操作失败");
    	}

    	return result;
    }
    
    /**
     * 
     * @Description 根据排序实现置顶功能，即修改当前排序为最大排序值加1，并且更新修改时间<br>
     * 
     * @param id 主键id
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年8月31日
     * @see
     */
    @RequestMapping(value = "updateSort", method = RequestMethod.GET)
    @ResponseBody
    public RequestJson updateSort(String id, HttpServletRequest req){
    	RequestJson result = new RequestJson();
    	try{
    		ShopBanner shopBanner = shopBannerService.selectByPrimaryKey(id);
    		if(shopBanner != null){
    			shopBanner.setUpdateTime(DateUtils.getDate());
    			shopBannerService.updateSort(shopBanner);
    		}
    	} catch (Exception e){
    		result.setSuccess(false);
    		result.setMsg("操作失败");
    		e.printStackTrace();
    	}

    	return result;
    }
    
    /**
     * 
     * @Description 根据主键id删除店铺装修记录<br>
     * 
     * @param id 主键id
     * @return 
     * @author lilan 1063972142@qq.com
     * @date 2016年8月31日
     * @see
     */
    @RequestMapping(value = "deleteStoreBanner", method = RequestMethod.GET)
    @ResponseBody
    public RequestJson deleteStoreBanner(String id, HttpServletRequest req){
    	RequestJson result = new RequestJson();
    	try{
    		shopBannerService.deleteByPrimaryKey(id);
    	} catch (Exception e){
    		result.setSuccess(false);
    		result.setMsg("操作失败");
    		e.printStackTrace();
    	}

    	return result;
    }
}
